package com.zh.work.controller.platform.trans;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.common.util.DateSetUtil;
import com.zh.work.entity.trans.OnlineTransHistoryEntity;
import com.zh.work.entity.trans.OnlineTransTodayEntity;
import com.zh.work.service.platform.trans.OnlineTransHistoryService;
import com.zh.work.service.platform.trans.OnlineTransTodayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description:
 * @Author: Li.HuiNeng
 * @Create in: 2020-01-06 11:43
 * @Version: 1.0.1
 */
@Api(value = "网关交易控制器", produces = "application/json", tags = {"网关交易控制器"})
@RestController
@RequestMapping("/onlineTrans")
public class OnlineTransController {

    @Autowired
    private OnlineTransTodayService serviceToday;


    @Autowired
    private OnlineTransHistoryService serviceHistory;

    @ApiOperation(value = "分页查询网关实时交易数据集", tags = {"getOnlineTodayPages"}, notes = "分页查询网关实时交易数据集")
    @GetMapping("/getOnlineTodayPages")
    public JSONResult<List<OnlineTransTodayEntity>> getOnlineTodayPages(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo") Integer pageSize,
            @ApiParam(value = "网关实时交易实体类", name = "param") OnlineTransTodayEntity param) {
        JSONResult<List<OnlineTransTodayEntity>> jsonResult = new JSONResult<>();
        try {
            Page<OnlineTransTodayEntity> page = new Page<>(pageNo, pageSize);
            IPage<OnlineTransTodayEntity> result = serviceToday.getOnlineTransPages(page, param);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "分页查询网关历史交易数据集", tags = {"getOnlineHistoryPages"}, notes = "分页查询网关历史交易数据集")
    @GetMapping("/getOnlineHistoryPages")
    public JSONResult<List<OnlineTransHistoryEntity>> getOnlineHistoryPages(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo") Integer pageSize,
            @ApiParam(value = "网关实时交易实体类", name = "param") OnlineTransHistoryEntity param) {
        JSONResult<List<OnlineTransHistoryEntity>> jsonResult = new JSONResult<>();
        try {
            Page<OnlineTransHistoryEntity> page = new Page<>(pageNo, pageSize);
            IPage<OnlineTransHistoryEntity> result = serviceHistory.getQrCodeTransHistoryPages(page, param);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "查询网关交易统计数据", tags = {"getOnlineTransStatistic"}, notes = "查询网关交易统计数据")
    @GetMapping("/getOnlineTransStatistic")
    public JSONResult<List<Map<String, String>>> getOnlineTransStatistic(@ApiParam(value = "网关交易实体类", name = "onlineTransTodayEntity")
                                                                                     OnlineTransTodayEntity onlineTransTodayEntity) {
        JSONResult<List<Map<String, String>>> jsonResult = new JSONResult<>();
        List<Map<String, String>> list = null;
        if (Objects.isNull(onlineTransTodayEntity.getStartDateTime())) {
            onlineTransTodayEntity.setStartDateTime(DateSetUtil.getDayFirstSec(new Date()));
        }
        if (Objects.isNull(onlineTransTodayEntity.getEndDateTime())) {
            onlineTransTodayEntity.setEndDateTime(DateSetUtil.getDayLastSec(new Date()));
        }
        try {
            list = this.serviceToday.getOnlineTransStatistic(onlineTransTodayEntity);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
        }
        if (list != null) {
            if (list.size() > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setData(list);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("统计数量为0");
            }
        } else {
            jsonResult.setSuccess(false);
            jsonResult.setMessage("统计数量为空");
        }
        return jsonResult;
    }

    @ApiOperation(value = "查询网关通知/充值统计数据", tags = {"getOnlineNotify"}, notes = "查询网关通知/充值统计数据")
    @GetMapping("/getOnlineNotify")
    public JSONResult<Map<String, String>> getOnlineNotify(@ApiParam(value = "网关交易实体类", name = "onlineTransTodayEntity")
                                                                   OnlineTransTodayEntity onlineTransTodayEntity) {
        JSONResult<Map<String, String>> jsonResult = new JSONResult<>();
        Map<String, String> map = null;
        if (Objects.isNull(onlineTransTodayEntity.getStartDateTime())) {
            onlineTransTodayEntity.setStartDateTime(DateSetUtil.getDayFirstSec(new Date()));
        }
        if (Objects.isNull(onlineTransTodayEntity.getEndDateTime())) {
            onlineTransTodayEntity.setEndDateTime(DateSetUtil.getDayLastSec(new Date()));
        }
        try {
            map = this.serviceToday.getOnlineNotify(onlineTransTodayEntity);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
        }
        if (map != null) {
            if (map.size() > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setData(map);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("统计数量为0");
            }
        } else {
            jsonResult.setSuccess(false);
            jsonResult.setMessage("统计数量为空");
        }
        return jsonResult;
    }

}
